package com.atguigu.spzx.product.service.impl;

import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.product.entity.Category;
import com.atguigu.spzx.product.entity.ProductSku;
import com.atguigu.spzx.product.manager.CategoryCacheManager;
import com.atguigu.spzx.product.mapper.CategoryMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.IndexService;
import com.atguigu.spzx.product.vo.IndexQueryRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * IndexServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/11/3
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private CategoryCacheManager categoryCacheManager;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    /**
     * 首页数据查询
     * @return
     */
    @Override
    public IndexQueryRespVo getIndexData() {
        //所有一级分类
        List<Category> OneLevelCategoryList = categoryCacheManager.getOneLevelCategoryList();
        List<IndexQueryRespVo.IndexQueryInnerCategory> categoryList = BeanUtils.toBeanList(OneLevelCategoryList, IndexQueryRespVo.IndexQueryInnerCategory.class);

        //根据销量排序，获取前十条记录
        List<ProductSku> skuList = productSkuMapper.getTop10ProductSkuListOrderBySaleNumDesc();
        List<IndexQueryRespVo.IndexQueryInnerProductSku> productSkuList = BeanUtils.toBeanList(skuList, IndexQueryRespVo.IndexQueryInnerProductSku.class);

        IndexQueryRespVo respVo = new IndexQueryRespVo();
        respVo.setCategoryList(categoryList);
        respVo.setProductSkuList(productSkuList);
        return respVo;
    }
}
